﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoadTeam.Enums;
using System.Data;
using RoadTeam.Model;
using RoadTeam.SysInterface;
using RoadTeam.DAL;

namespace RoadTeam.BsSystem.CS.HomeInspection
{
    public class TbCsValidatePro_Sys : ICsValidatePro
    {
        public String GetCsValidateProID(String Identifier)
        {
            SqlWhere where = new SqlWhere();
            where.Info = new TbCsValidatePro() { App_Identifier = Identifier };
            where.SelectColumn = @" top 1 validateproID  ";

            DataTable dt = TbCsValidatePro.Select(where, GlobalConfig.RtConnectionString);

            if (dt.Rows.Count == 0) return @"-1";
            return dt.Rows[0][0].ToString();
        }

        public DataTable GetValidateProTable(String OrganID)
        {
            SqlWhere where = new SqlWhere();
            where.Info = new TbCsValidatePro() { OrganID = Convert.ToDecimal(OrganID) };
            where.StrSqlWhere = @"   App_Identifier Is  NULL   ";
            where.SelectColumn = @"  ValidateProName,ValidateObjectID,ValidateProCode,App_Identifier,OrganID,ValidateProID ";
            return TbCsValidatePro.Select(where, GlobalConfig.RtConnectionString);
        }


        public bool UpdateValidateProTable(DataTable dt)
        {
            return TbCsValidatePro.Update(dt, GlobalConfig.RtConnectionString);
        }

        public DataTable  GetValidateProDataByIDs(String ProIDs)
        {
            SqlWhere where = new SqlWhere();
            where.StrSqlWhere = @"   ValidateProID  in (" + ProIDs + ")   ";
            where.SelectColumn = @"  ValidateProName,ValidateObjectID,ValidateProCode,App_Identifier,OrganID,ValidateProID ";
            return TbCsValidatePro.Select(where, GlobalConfig.RtConnectionString);
        }




        public DataTable GetValidateProDataByPage(String OrganID, int PageSize, int CurrentPage, int AllCount)
        {
            String Str = @"
SELECT
TbCsDoorType.DoorTypeID,
TbCsDoorType.DoorTypeName,
TbCsDoorType.DoorTypeCode,
TbCsDoorType.App_Identifier AS DoorTypeIdentifier,

TbCsRoomType.RoomTypeID,
TbCsRoomType.RoomTypeName,
TbCsRoomType.RoomTypeCode,
TbCsRoomType.App_Identifier AS  RoomTypeIdentifier,

TbCsValidateObject.ValidateObjectID,
TbCsValidateObject.ValidateObjectName,
TbCsValidateObject.ValidateObjectCode,
TbCsValidateObject.ProviderID,
TbCsValidateObject.MaintenanceTime,
TbCsValidateObject.App_Identifier AS  ValidateObjectIdentifier,

TbCsValidatePro.ValidateProID,
TbCsValidatePro.ValidateProName,
TbCsValidatePro.ValidateProCode,
TbCsValidatePro.OrganID,
TbCsValidatePro.App_Identifier AS ValidateProIdentifier ,

TbCsRoomObjectRela.RelaID AS RoObjectRela,
TbCsDoorRoomRela.RelaID AS DoRoomRela,
TbSmOrgan.OrganIdentifier
FROM   TbCsDoorType
Inner JOIN TbCsDoorRoomRela 
on TbCsDoorRoomRela.DoorTypeID=TbCsDoorType.DoorTypeID
INNER JOIN TbCsRoomType
on TbCsRoomType.RoomTypeID=TbCsDoorRoomRela.RoomTypeID
INNER JOIN TbCsRoomObjectRela
on TbCsRoomObjectRela.RoomTypeID=TbCsRoomType.RoomTypeID
INNER JOIN TbCsValidateObject 
on TbCsValidateObject.ValidateObjectID=TbCsRoomObjectRela.ValidateObjectID
INNER JOIN TbCsValidatePro 
on TbCsValidatePro.ValidateObjectID=TbCsValidateObject.ValidateObjectID
INNER JOIN TbSmOrgan  on TbSmOrgan.OrganID=TbCsDoorType.OrganID
WHERE TbCsDoorType.OrganID=" + OrganID + "  ORDER BY DoorTypeName,RoomTypeName";


         String StrSql = SQLPage.MakeSQLStringByPage(Str, String.Empty, PageSize, CurrentPage, AllCount);

         DataTable dt = DbFactory.CreateInstance().CreateDb().
         ExecuteStrQuery(StrSql, CommandType.Text, GlobalConfig.RtConnectionString);
         return dt;
        }

        public int GetValidateProDataAllPageCount(String OrganID)
        {
            String Str = @"
SELECT count (*) as  counts from (
SELECT 
TbCsDoorType.*
FROM   TbCsDoorType
Inner JOIN TbCsDoorRoomRela 
on TbCsDoorRoomRela.DoorTypeID=TbCsDoorType.DoorTypeID
INNER JOIN TbCsRoomType
on TbCsRoomType.RoomTypeID=TbCsDoorRoomRela.RoomTypeID
INNER JOIN TbCsRoomObjectRela
on TbCsRoomObjectRela.RoomTypeID=TbCsRoomType.RoomTypeID
INNER JOIN TbCsValidateObject 
on TbCsValidateObject.ValidateObjectID=TbCsRoomObjectRela.ValidateObjectID
INNER JOIN TbCsValidatePro 
on TbCsValidatePro.ValidateObjectID=TbCsValidateObject.ValidateObjectID
WHERE TbCsDoorType.OrganID=" + OrganID + ") as table1";

            DataTable dt = DbFactory.CreateInstance().CreateDb().
            ExecuteStrQuery(Str, CommandType.Text, GlobalConfig.RtConnectionString);
            return Convert.ToInt32(dt.Rows[0][0]);
        }
    }
}
